package com.xpec.c4.db.usertype;

import java.util.ArrayList;
import java.util.List;

import com.ms.db.usertype.BasicToString;
import com.xpec.c4.db.util.CodecTool;

/**
 * 把ArrayList<String>转换为String<br>
 * 使用指定的分隔符，默认为";"
 * 
 * @author Bany
 * 
 */
public class ArrayListStringToString extends BasicToString {


	/**
	 * 
	 */
	private static final long serialVersionUID = 5822642227889260444L;

	@SuppressWarnings("unchecked")
	@Override
	protected String assemble(Object obj) {
		List<String> arrayList = (List<String>)obj;
		StringBuilder strBui = new StringBuilder();
		for (int i = 0; i < arrayList.size() - 1; i++) {
			CodecTool.append(strBui,arrayList.get(i),SPLITTER);
		}
		if (arrayList.size() > 0) {
			strBui.append(arrayList.get(arrayList.size() - 1));
		}
		return strBui.toString();
	}

	@Override
	protected Object parse(String value) {
		if(value==null){
			return null;
		}
		List<String> list = new ArrayList<String>();
		if (value == null || value.length() <= 0) {
			return list;
		}
		String[] strs=CodecTool.split(value, SPLITTER);
		for (int i = 0; i < strs.length; i++) {
			list.add(strs[i]);
		}
		return list;
	}

	
	public int[] sqlTypes() {

		return CLOB_TYPES;
	}

	public Class<?> returnedClass() {
		return List.class;
	}
}
